package com.threadExample;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by yijy on 2018/4/19.
 */
public class MyTask implements Runnable{

    private boolean needFlag;

    private static final ThreadLocal<ThreadLocalDemo.Person> aPerson = new ThreadLocal<ThreadLocalDemo.Person>(){
        @Override
        protected ThreadLocalDemo.Person initialValue() {
            return new ThreadLocalDemo.Person("A body",0);
        }
    };

    public MyTask(boolean needFlag) {
        this.needFlag = needFlag;
    }

    Random random = new Random(3);

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+":start......");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(needFlag){
            aPerson.set(new ThreadLocalDemo.Person(Thread.currentThread().getName(),random.nextInt()));
        }
        System.out.println("This's ThreadLocal :"+aPerson.get());

    }
}
